$(function () {
    var trace_datagrid = $("#trace_datagrid");
    var trace_detailed_dialog = $("#trace_detailed_dialog");
    var trace_detailed_form = $("#trace_detailed_form");
    var audit_dialog = $("#audit_dialog");
    var audit_form = $("#audit_form");

    // 统一方法对象
    var cmdObj = {
        // 刷新跟进
        reloadTrace: function () {
            trace_datagrid.datagrid('reload');
        },
        // 查看详情
        detailsTrace: function () {
            var currentRow = trace_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请选择一行数据', 'error');
                return;
            }
            trace_detailed_form.form('clear');

            if (currentRow.client) {
                currentRow['client.name'] = currentRow.client.name;
            }

            if (currentRow.traceEmployee) {
                currentRow['traceEmployee.username'] = currentRow.traceEmployee.username;
            }

            trace_detailed_form.form('load', currentRow);
            trace_detailed_dialog.dialog('open');
        },
        // 审核弹框
        auditTrace: function () {
            var currentRow = trace_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请选择一行审核数据', 'error');
                return;
            }
            if (currentRow.status) {
                $.messager.alert('提示', '该跟进已被审核,请勿重复审核', 'error');
                return;
            }
            audit_dialog.dialog('open');
        },
        // 关闭详情框
        detailed_close: function () {
            trace_detailed_dialog.dialog('close');
        },
        // 审核提交
        audit_ok: function () {
            $.messager.confirm('提示', '确定要审核吗?', function (ret) {
                if (ret) {
                    var traceId = trace_datagrid.datagrid('getSelected').id;
                    var level = $("#audit_level").val();
                    var auditAssess = $("#auditAssess").val();
                    $.post('/trace/audit', {traceId: traceId, level: level, auditAssess: auditAssess}, function (data) {
                        if (data.success) {
                            $.messager.alert('提示', '审核成功', 'info');
                            audit_dialog.dialog('close');
                            cmdObj.reloadTrace();
                        } else {
                            $.messager.alert('提示', data.msg, 'error');
                        }
                    });
                }
            });
        },
        // 审核取消
        audit_cancel: function () {
            audit_dialog.dialog('close');
        }
    }

    // 统一方法调用
    $("a[data-cmd]").click(function () {
        cmdObj[$(this).data('cmd')]();
    });

    trace_datagrid.datagrid({
        url: '/trace/query',
        fit: true,
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        width: 1000,
        columns: [[
            {field: 'id', title: '编号', halign: 'center', align: 'center', width: 100, hidden: true},
            {field: 'client', title: '客户名', halign: 'center', align: 'center', width: 100, formatter: formatClient},
            {
                field: 'traceEmployee', title: '跟进人', halign: 'center', align: 'center', width: 100,
                formatter: formatTraceEmployee
            },
            {field: 'traceDate', title: '跟进时间', halign: 'center', align: 'center', width: 100},
            {field: 'traceMatter', title: '跟进问题', halign: 'center', align: 'center', width: 100},
            {field: 'synopsis', title: '跟进摘要', halign: 'center', align: 'center', width: 100},
            {field: 'status', title: '审核状态', halign: 'center', align: 'center', width: 100, formatter: formatStatus},
            {
                field: 'assessLevel', title: '审核等级', halign: 'center', align: 'center', width: 100,
                formatter: formatAssessLevel
            },
            {field: 'auditAssess', title: '审核评价', halign: 'center', align: 'center', width: 100}
        ]],
        toolbar: '#trace_datagrid_toolbar'
    })
});

function formatClient(value, row, index) {
    return value == null ? '' : value.name;
}
function formatAssessLevel(value, row, index) {
    return value == null ? '' : value.name;
}
function formatTraceEmployee(value, row, index) {
    return value == null ? '' : value.username;
}
function formatStatus(value, row, index) {
    return value == 0 ? "<span style='color: red'>" + '未审核' + "</span>" : "<span style='color: green'>" + '已审核' + "</span>";
}




















